package com.tianzeculture.stagebao.base;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.tianzeculture.stagebao.activity.login.LoginActivity;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.bean.User;
import com.tianzeculture.stagebao.net.CommonRestUsage;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.NetWorkInject;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.task.ITask;
import com.tianzeculture.stagebao.network.task.IUIController;
import com.tianzeculture.stagebao.network.task.TaskManager;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.D;
import com.tianzeculture.stagebao.util.StatusBarUtil;
import com.tianzeculture.stagebao.util.Util;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/5/23 0023.
 */

public abstract class BaseActivity extends AppCompatActivity implements IUIController {
    /**
     * 加载中Dialog
     */
    private Dialog progressDialog;
    private Unbinder unbinder;

    private int getUserInfo = 8001;//获取个人信息
    private int getindustryList = 8002;//获取职业列表
    private int getWorkTypeList = 8003;//获取工作类型列表


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onCreate(savedInstanceState, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        StatusBarUtil2.StatusBarLightMode(this);
        StatusBarUtil.setStatusBarLightMode(this);
//        StatusBarUtil.setWindowStatusBarColor(this, R.color.colorGray_F8);


    }

    protected void onCreate(Bundle savedInstanceState, int requestedOrientation) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (requestedOrientation != -2) {
            setRequestedOrientation(requestedOrientation);
        }
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

        NetWorkInject.init(this);

        TaskManager.getInstance().registerUIController(this);

        int layoutResId = getLayoutResID();
        if (layoutResId != View.NO_ID) {
            setContentView(layoutResId);

            unbinder = ButterKnife.bind(this);

            initUI();
        }

//        requestUserInfo();
//        requestIndustryList();
//        requestWorkTypeList();
    }

//    /**
//     * 获取个人信息
//     */
//    private void requestUserInfo() {
//        if ("".equals(Util.UserId)) {
//            return;
//        }
//        UserRestUsage userRestUsage = new UserRestUsage();
//        userRestUsage.setIdentification(getIdentification());
//        userRestUsage.myInfo(getUserInfo, Util.UserId);
//    }

//    /**
//     * 职业列表
//     */
//    private void requestIndustryList() {
//        CommonRestUsage commonRestUsage = new CommonRestUsage();
//        commonRestUsage.setIdentification(getIdentification());
//        commonRestUsage.industryList(getindustryList);
//    }

//    /**
//     * 工作类型
//     */
//    private void requestWorkTypeList() {
//        CommonRestUsage commonRestUsage = new CommonRestUsage();
//        commonRestUsage.setIdentification(getIdentification());
//        commonRestUsage.workTypeList(getWorkTypeList);
//    }


    protected abstract int getLayoutResID();

    @Override
    public void initUI() {

    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        if (dealMSG(msg))
            startActivity(new Intent(this, LoginActivity.class));
            return;

//        if (taskId == getUserInfo) {
//            if (msg.getData()) {
//                ResultBean resultBean = (ResultBean) msg.getObj();
//                User user = resultBean.getUser();
//                if (user != null)
//                    Util.user = user;
//            }
//        } else if (taskId == getindustryList) {
//            if (msg.getData()) {
//                ResultBean resultBean = (ResultBean) msg.getObj();
//                Util.allJobBeen = resultBean.getList();
//            }
//        } else if (taskId == getWorkTypeList) {
//            if (msg.getData()) {
//                ResultBean resultBean = (ResultBean) msg.getObj();
//                Util.allWorkTypeBeen = resultBean.getWorklist();
//            }
//        }

    }

    @Override
    public String getIdentification() {
        return getClass().toString() + this;
    }

    /**
     * 执行Task任务
     *
     * @param task
     */
    protected void execuTask(ITask task) {
        if (task == null)
            return;
        task.setContext(this);
        task.setmIdentification(getIdentification());
        TaskManager.getInstance().addTask(task);
    }


    /**
     * 返回
     *
     * @param view
     */
    public void backClick(View view) {
        finish();
    }


    /**
     * 处理 MSG
     *
     * @param msg
     * @return
     */
    protected boolean dealMSG(MSG msg) {

        if (!msg.isFromCache() && "账号异常".equals(msg.getMessage())) {  //token过期或者未登录    忽略缓存的 code
            ToastUtil.showShort(this, "账号过期");
            return true;
        } else
            return false;
    }


    /**
     * 显示Progress   返回键可以dismiss
     */
    protected void showProgressDialog() {
        showProgressDialog("加载中...");
    }


    /**
     * 显示Progress   返回键可以dismiss
     */
    protected void showProgressDialog(DialogInterface.OnDismissListener onDismissListener) {
        showProgressDialog("加载中...", true, onDismissListener);
    }

    /**
     * 显示Progress   返回键可以dismiss
     */
    protected void showProgressDialogNoCancel() {
        showProgressDialog("加载中...", false);
    }

    /**
     * 显示Progress   返回键可以dismiss
     */
    protected void showProgressDialog(String message) {
        showProgressDialog(message, true);
    }

    /**
     * 显示Progress
     *
     * @param isCancelable 返回键是否可以dismiss
     */
    protected void showProgressDialog(String message, boolean isCancelable) {
        showProgressDialog(message, isCancelable, null);
    }

    /**
     * 显示Progress
     *
     * @param isCancelable 返回键是否可以dismiss
     */
    protected void showProgressDialog(String message, boolean isCancelable, DialogInterface.OnDismissListener onDismissListener) {
        dismissProgressDialog();
        if (isFinishing()) {
            return;
        }
        progressDialog = D.getProgressDialog(this, message, isCancelable);
        if (progressDialog != null) {
            progressDialog.setOnDismissListener(onDismissListener);
            progressDialog.show();
        }
    }

    /**
     * 关闭Progress
     */
    protected void dismissProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        progressDialog = null;
    }


//    @Override
//    public void onWindowFocusChanged(boolean hasFocus) {
//        super.onWindowFocusChanged(hasFocus);
//        if (hasFocus && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            View decorView = getWindow().getDecorView();
//            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//        }
//    }
}